package com.huawei.app.demo.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.huawei.app.demo.util.LogUtils;

/**
 * 处理应用的安装卸载升级
 *
 * @since 2021-04-27
 */
public class AppAddRemoveEventReceiver extends BroadcastReceiver {

    private static final String TAG = AppAddRemoveEventReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String packageName = intent.getData().getSchemeSpecificPart();

        if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
            String msg = "install package " + packageName;
            LogUtils.getInstance().i(TAG, msg);
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
            String msg = "uninstall package " + packageName;
            boolean isReplacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false);
            if (isReplacing) {
                msg = "replacing package " + packageName;
            }
            LogUtils.getInstance().i(TAG, msg);
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
            String msg = "replaced package " + packageName;
            LogUtils.getInstance().i(TAG, msg);
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    }
}
